package cheme.ProcessModel;

import java.beans.PropertyChangeEvent;

/*
 * Extends the basic PropertyChangeEvent to allow nesting of events. Thus far
 * this is used not to string together long chains of events but to provide
 * composite events (those that originate from an element and extend to include
 * its parent element) with a major type (the top-level event type) and a minor
 * type (the inner event's event type). This mechanism is used to communicate
 * events that are both actionable by the GEF part/presentation layer and
 * interpretable by the interpreter.
 */

public class ChemePropertyChangeEvent extends PropertyChangeEvent
{
	/*
	 * Serialization version identifier. (Set to default for now)
	 */
	
	private static final long serialVersionUID = 1L;

	private PropertyChangeEvent subevent;

	public ChemePropertyChangeEvent(PropertyChangeEvent basis)
	{
		super(basis.getSource(), basis.getPropertyName(), basis.getOldValue(), basis.getNewValue());
	}
	
	public ChemePropertyChangeEvent(Object source, String propName, Object oldValue, Object newValue)
	{
		super(source, propName, oldValue, newValue);
	}
	
	public ChemePropertyChangeEvent(Object source, String propName, Object oldValue, Object newValue, PropertyChangeEvent subevent)
	{
		super(source, propName, oldValue, newValue);
		
		this.subevent = subevent;
	}
	
	public PropertyChangeEvent getSubevent()
	{
		return subevent;
	}
	
	public boolean includesEvent(String eventName)
	{
		boolean res = getPropertyName().equals(eventName);
		
		res |= (null != subevent) && 
				(
					(subevent.getPropertyName().equals(eventName) ||
					subevent instanceof ChemePropertyChangeEvent && ((ChemePropertyChangeEvent)subevent).includesEvent(eventName))
				);
		
		return res;
	}
	
	public static PropertyChangeEvent getSubevenFromt(String eventName, PropertyChangeEvent evt)
	{
		PropertyChangeEvent result = null;
		
		if(evt.getPropertyName().equals(eventName))
		{
			result = evt;
		}
		else if(evt instanceof ChemePropertyChangeEvent)
		{
			result = getSubevenFromt(eventName, ((ChemePropertyChangeEvent)evt).getSubevent());
		}
		
		return result;
	}
}
